package org.aviran.cookiebar2.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Button;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.window.service.WindowManager;
import org.aviran.cookiebar2.CookieBottomView;
import org.aviran.cookiebar2.CookieTopView;
import org.aviran.cookiebar2.ResourceTable;
import org.aviran.cookiebar2.Util.StringUtil;

/**
 * MainAbilitySlice
 *
 * @since : 2021.5.24
 */
public class MainAbilitySlice extends AbilitySlice {
    private CookieTopView cookieTopView;
    private DependentLayout rootComponent;
    private CookieBottomView cookieBottomView;

    private Button topCookieButton;
    private Text displayText;
    private Button bottomCookie;
    private Button customButton;
    private Button fancyBottomCookie;
    private Button customViewCookie;

    private String message = "This is a top cookie with an icon (#0)";
    private StringUtil stringUtil;

    @Override
    public void onStart(Intent intent) {
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        stringUtil = new StringUtil();
        // 点击空白处收回CookieBar2
        initComponent();
        initBt();
        rootComponent.setClickedListener(component -> {
            if (cookieTopView.isTopExist) {
                cookieTopView.isNeedTopToBottom = false;
                cookieTopView.startAnimationBottom();
                cookieTopView.isTopExist = false;
                displayText.setText("Cookie dismissed programmatically");
            }
            if (cookieTopView.isLeftExit) {
                cookieTopView.isNeedLeftToRight = false;
                cookieTopView.startAnimationRight();
                cookieTopView.isLeftExit = false;
            }
            if (cookieBottomView.isExit) {
                cookieBottomView.removeHandler();
                cookieBottomView.isNeedTopToBottom = false;
                cookieBottomView.startAnimationBottom();
            }
        });
    }

    private void initComponent() {
        if (findComponentById(ResourceTable.Id_cookieTopView) instanceof CookieTopView) {
            cookieTopView = (CookieTopView) findComponentById(ResourceTable.Id_cookieTopView);
        }
        if (findComponentById(ResourceTable.Id_rootComponent) instanceof DependentLayout) {
            rootComponent = (DependentLayout) findComponentById(ResourceTable.Id_rootComponent);
        }
        if (findComponentById(ResourceTable.Id_myText) instanceof Text) {
            displayText = (Text) findComponentById(ResourceTable.Id_myText);
        }
        cookieTopView.setDisplayText(displayText);
        if (findComponentById(ResourceTable.Id_cookieBottomView) instanceof CookieBottomView) {
            cookieBottomView = (CookieBottomView) findComponentById(ResourceTable.Id_cookieBottomView);
        }
        cookieBottomView.setCookieBottomView(cookieBottomView);
        cookieBottomView.setDisplayText(displayText);
        cookieTopView.setCookieBottomView(cookieBottomView);
        if (findComponentById(ResourceTable.Id_topCookie) instanceof Button) {
            topCookieButton = (Button) findComponentById(ResourceTable.Id_topCookie);
        }
        if (findComponentById(ResourceTable.Id_bottomCookie) instanceof Button) {
            bottomCookie = (Button) findComponentById(ResourceTable.Id_bottomCookie);
        }
        if (findComponentById(ResourceTable.Id_customCookie) instanceof Button) {
            customButton = (Button) findComponentById(ResourceTable.Id_customCookie);
        }
        if (findComponentById(ResourceTable.Id_fancyBottomCookie) instanceof Button) {
            fancyBottomCookie = (Button) findComponentById(ResourceTable.Id_fancyBottomCookie);
        }
        if (findComponentById(ResourceTable.Id_customViewCookie) instanceof Button) {
            customViewCookie = (Button) findComponentById(ResourceTable.Id_customViewCookie);
        }
    }

    private void initBt() {
        initCookieTopView();
        initCookieBottomView();
    }

    private void initCookieTopView() {
        topCookieButton.setClickedListener(component -> {
            message = stringUtil.stringNumberIncrease(message);
            cookieTopView.setIcon(ResourceTable.Media_icon);
            cookieTopView.setDuration(5000);
            cookieTopView.setTitle("Top cookie with an icon!");
            cookieTopView.setTitleColor(Color.YELLOW);
            cookieTopView.setMessage(message);
            cookieTopView.setMessageColor(Color.WHITE);
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(new RgbColor(171, 71, 188));
            cookieTopView.setBackgroundColor(shapeElement);
            cookieTopView.startAnimationTopToBottom();
        });
        customButton.setClickedListener(component -> {
            cookieTopView.setIcon(ResourceTable.Media_icon);
            cookieTopView.setDuration(5000);
            cookieTopView.setTitle("This cookie has custom in/out animations");
            cookieTopView.setTitleColor(Color.WHITE);
            cookieTopView.setMessage("Did you notice it entered from the left?" +
                    "Can you guess how it will leave the screen?If you guessed from the right, you're right!");
            cookieTopView.setMessageColor(Color.WHITE);
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(new RgbColor(255, 138, 101));
            cookieTopView.setBackgroundColor(shapeElement);
            cookieTopView.startAnimationLeftToRight();
        });
    }

    private void initCookieBottomView() {
        bottomCookie.setClickedListener(component -> {
            cookieBottomView.setDuration(5000);
            cookieBottomView.setTitle("Bottom cookie with and icon");
            cookieBottomView.setTitleColor(Color.YELLOW);
            cookieBottomView.setMessage("This is a bottom cookie with an icon and an action. Nice right?");
            cookieBottomView.setMessageColor(Color.WHITE);
            cookieBottomView.setAction("Action");
            cookieBottomView.setActionColor(Color.YELLOW);
            cookieBottomView.setIcon(ResourceTable.Media_icon);
            cookieBottomView.showThreeStatusButton(false); // 是否需要显示三个状态按钮
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(new RgbColor(33, 150, 243));
            cookieBottomView.setBackgroundColor(shapeElement);
            cookieBottomView.startAnimationBottomToTop();
        });
        fancyBottomCookie.setClickedListener(component -> {
            cookieBottomView.setDuration(5000);
            cookieBottomView.setTitle("Icons can be animated");
            cookieBottomView.setTitleColor(Color.YELLOW);
            cookieBottomView.setMessage("Fancy animations will make your cookies super exciting.");
            cookieTopView.setMessageColor(Color.WHITE);
            cookieBottomView.setAction("OPEN SETTINGS");
            cookieBottomView.setActionColor(Color.YELLOW);
            cookieBottomView.setIcon(ResourceTable.Media_ic_settings_white_48dp);
            cookieBottomView.showThreeStatusButton(false); // 是否需要显示三个状态按钮
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(new RgbColor(76, 175, 80));
            cookieBottomView.setBackgroundColor(shapeElement);
            cookieBottomView.startAnimationBottomToTop();
        });
        customViewCookie.setClickedListener(component -> {
            cookieBottomView.setTitle("Custom cookie view demo!");
            cookieBottomView.setTitleColor(Color.WHITE);
            cookieBottomView.setMessage("You can customize CookieBar2. Here's an example of a simple dialog.");
            cookieTopView.setMessageColor(Color.WHITE);
            cookieBottomView.setAction("CLOSE");
            Color color = new Color(Color.getIntColor("#26a69a"));
            cookieBottomView.setActionColor(color);
            cookieBottomView.setIcon(ResourceTable.Media_icon);
            cookieBottomView.showThreeStatusButton(true); // 是否需要显示三个状态按钮
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(new RgbColor(63, 81, 181));
            cookieBottomView.setBackgroundColor(shapeElement);
            cookieBottomView.startAnimationBottomToTop();
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

}
